<template>
  <div class="hotelAdmin">
    <h1>酒店信息列表</h1>

    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-input prefix-icon="el-icon-search" v-model="formInline.searchInfo" @keyup.enter.native="search(formInline.searchInfo,formInline.region)"
          placeholder="搜索名称,酒店"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select @input="search(formInline.searchInfo,formInline.region)" v-model="formInline.region" placeholder="全部状态">
          <el-option label="已发布" value="1"></el-option>
          <el-option label="未发布" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search(formInline.searchInfo,formInline.region)">搜索</el-button>
        <el-button type="primary" @click="handleEdit">添加案例</el-button>
      </el-form-item>
    </el-form>


    <el-table :data="tableData" style="width: 100%">

      <el-table-column label="酒店名称" width="180" align="center">
        <template slot-scope="scope">
          <span >{{ scope.row.hotelname }}</span>
        </template>
      </el-table-column>
        <el-table-column label="ID" width="180" align="center">
        <template slot-scope="scope">
          <span >{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="180" align="center">
        <template slot-scope="scope">
          <span  v-if="scope.row.ifrelease==1">已发布</span>
          <span  v-else>未发布</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row.id)">编辑</el-button>
          <el-button size="mini" type="primary" v-if="scope.row.ifrelease==1" @click="handleDelete(scope.row.id, scope.row.ifrelease)">取消发布</el-button>
          <el-button size="mini" type="primary" v-else @click="handleDelete(scope.row.id, scope.row.ifrelease)">发布</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="text-align: center;" background @current-change="handleCurrentChange" :current-page.sync="currentpage" :total="total">
   </el-pagination>
  </div>
  </div>
</template>

<script>
  export default {
    name: 'hotelAdmin',
    data() {
      return {
        formInline: {
          searchInfo: '', //搜索
          region: '' //发布状态
        },
        tableData: [],
        total: 1,
        pagesize: 10,
        currentpage: null
      }
    },
    created() {
      this.api.get('/api/searchhotel').then(res => {
        this.total = res.data.total;
        this.tableData = res.data.array
      })
    },
    methods: {
      handleEdit(index, id) {
        this.$router.push({
          path: './editHotel',
          query: {
            infoId: id
          }
        })
      },
      handleDelete(index, status) {

        if (status == 2) {
          status = 1
        } else {
          status = 2

        }
        let param = {
          id: index,
          ifrelease: status
        };
        this.api.get('/api/cancelhotel', param).then(res => {
          console.log(res.data);
          this.total = res.data.total;
          this.tableData = res.data
        })

      },
      search(keyword, status) {
        let param = {
          count: 10,
          keyword: keyword,
          ifrelease: status
        }
        this.api.get('/api/searchhotel', param).then(res => {
          this.tableData = res.data.array
        })
      },
      handleCurrentChange(currentpage) {
        let keyword = this.formInline.searchInfo;
        let status = this.formInline.region;
        if (keyword != "" || status != "") {
          this.api.get('/api/searchhotel?keyword=' + keyword + '&ifrelease=' + status + '&page=' + currentpage).then(res => {
            this.total = res.data.total;
            this.tableData = res.data.array
          })
        } else {
          this.api.get('/api/searchhotel', {
            page: currentpage
          }).then(res => {
            this.total = res.data.total;
            this.tableData = res.data.array;
          })
        }

      }
    }

  }
</script>

<style scoped>

</style>
